import { defineStore } from 'pinia'
import request from '@/utils/request'
import { ApiRes, CategoryItem, TopCategory ,SubCategory} from '@/types/data'
import { topCategory } from '../constants'
const defaultCategory = topCategory.map(item => {
  return {
    name:item,
  }
})
export default defineStore('category', {
  state () {
    return {
      list: defaultCategory as CategoryItem[],
      topCategory:{} as TopCategory,
      subCategory: {} as SubCategory,
    }
  },
 actions:{
  async getAllCategory() {
    // const res = await request('home/category/head')
    const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
    // 这里对后台数据返回 回来的数据进行加工，新增一个属性open，标记弹层的显示隐藏
    // 默认所有都隐藏
    res.data.result.forEach(item => {
      item.open=false
    });
    this.list = res.data.result
    // console.log(res);
  },
  async getTopCategory(id:string) {
    const res = await request.get<ApiRes<TopCategory>>('/category',{
      params:{
        id,
      },
    })
    this.topCategory = res.data.result
  },
  async getSubFilter(id: string) {
    const res = await request.get<ApiRes<SubCategory>>(
      '/category/sub/filter',
      {
        params: {
          id,
        },
      }
    )
    this.subCategory = res.data.result
  },

  show (id: string) {
   const itemObj= this.list.find(item=>item.id === id)
   itemObj!.open = true
  },
  hide (id: string) {
    const itemObj= this.list.find(item=>item.id === id)
   itemObj!.open = false
  }
 },

  
  
})